package com.gis507.test.AISDataToMysql;

import com.gis507.test.AISDataSort.AISDataBean;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AISInterBean implements Writable, DBWritable {

    private String mmsi;
    private double speed;
    private long unixTime;
    private double lon_d;
    private double lat_d;
    private String draught;
    private String length;
    private String width;
    private String callsign;
    private double course;
    private double rot;

    public AISInterBean(String mmsi,double speed, long unixTime, double lon_d, double lat_d, String draught, String length, String width, String callsign, double course, double rot) {
        this.mmsi = mmsi;
        this.speed = speed;
        this.unixTime = unixTime;
        this.lon_d = lon_d;
        this.lat_d = lat_d;
        this.draught = draught;
        this.length = length;
        this.width = width;
        this.callsign = callsign;
//        this.heading = heading;
        this.course = course;
        this.rot = rot;
    }

    public String getMmsi() {
        return mmsi;
    }

    public void setMmsi(String mmsi) {
        this.mmsi = mmsi;
    }

    public AISInterBean() {
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public long getUnixTime() {
        return unixTime;
    }

    public void setUnixTime(long unixTime) {
        this.unixTime = unixTime;
    }

    public double getLon_d() {
        return lon_d;
    }

    public void setLon_d(double lon_d) {
        this.lon_d = lon_d;
    }

    public double getLat_d() {
        return lat_d;
    }

    public void setLat_d(double lat_d) {
        this.lat_d = lat_d;
    }

    public String getDraught() {
        return draught;
    }

    public void setDraught(String draught) {
        this.draught = draught;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getCallsign() {
        return callsign;
    }

    public void setCallsign(String callsign) {
        this.callsign = callsign;
    }

//    public double getHeading() {
//        return heading;
//    }
//
//    public void setHeading(double heading) {
//        this.heading = heading;
//    }

    public double getCourse() {
        return course;
    }

    public void setCourse(double course) {
        this.course = course;
    }

    public double getRot() {
        return rot;
    }

    public void setRot(double rot) {
        this.rot = rot;
    }

    @Override
    public String toString() {
        return mmsi + "," + speed + "," + unixTime + "," + lon_d + "," + lat_d + "," + draught + ","
                + length + "," + width + "," + callsign + "," + course + "," + rot ;
    }


    @Override
    public void write(DataOutput dataOutput) throws IOException {

        //mmsi.speed，unixTime，lon_d，lat_d，(String)draught(类型),Length,Width,CallSign,Heading,Course,Rot
        dataOutput.writeUTF(mmsi);
        dataOutput.writeDouble(speed);
        dataOutput.writeLong(unixTime);
        dataOutput.writeDouble(lon_d);
        dataOutput.writeDouble(lat_d);
        dataOutput.writeUTF(draught);
        dataOutput.writeUTF(length);
        dataOutput.writeUTF(width);
        dataOutput.writeUTF(callsign);
//        dataOutput.writeDouble(heading);
        dataOutput.writeDouble(course);
        dataOutput.writeDouble(rot);

    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {

        this.mmsi = dataInput.readUTF();
        this.speed = dataInput.readDouble();
        this.unixTime = dataInput.readLong();
        this.lon_d = dataInput.readDouble();
        this.lat_d = dataInput.readDouble();
        this.draught = dataInput.readUTF();
        this.length = dataInput.readUTF();
        this.width = dataInput.readUTF();
        this.callsign = dataInput.readUTF();
//        this.heading = dataInput.readDouble();
        this.course = dataInput.readDouble();
        this.rot = dataInput.readDouble();

    }

    //    @Override
    public int compareTo(AISDataBean o) {

        if (this.getUnixTime() > o.getUnixTime()){
            return 1;
        }else{
            return -1;
        }
    }

    //    private String mmsi;
    //    private double speed;
    //    private long unixTime;
    //    private double lon_d;
    //    private double lat_d;
    //    private String draught;
    //    private String length;
    //    private String width;
    //    private String callsign;
    //    private double course;
    //    private double rot;
    @Override
    public void write(PreparedStatement statement) throws SQLException {
        statement.setString(1,this.mmsi);
        statement.setDouble(2,this.speed);
        statement.setLong(3,this.unixTime);
        statement.setDouble(4,this.lon_d);
        statement.setDouble(5,this.lat_d);
        statement.setString(6,this.draught);
        statement.setString(7,this.length);
        statement.setString(8,this.width);
        statement.setString(9,this.callsign);
        statement.setDouble(10,this.course);
        statement.setDouble(11,this.rot);
    }

    @Override
    public void readFields(ResultSet resultSet) throws SQLException {

        this.mmsi = resultSet.getString(1);
        this.speed = resultSet.getDouble(2);
        this.unixTime = resultSet.getLong(3);
        this.lon_d = resultSet.getDouble(4);
        this.lat_d = resultSet.getDouble(5);
        this.draught = resultSet.getString(6);
        this.length = resultSet.getString(7);
        this.width = resultSet.getString(8);
        this.callsign = resultSet.getString(9);
        this.course = resultSet.getDouble(10);
        this.rot = resultSet.getDouble(11);

    }
}
